package com.android.base.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.base.R;


/**
 * Created by mushuai on 2016/10/13.
 */
public class ToastShow {

    private Context context;
    public static Toast toast = null;

    public static ToastShow mToastShow;
    public static final int INFO_FLAG = 0;  // 基本提示
    public static final int ERROR_FLAG = 1;   // 错误提示
    public static final int NETWORK_FLAG = 2; //网络错误提示
    public static final int SUCCESS_FLAG = 3;  // 成功提示
    public static final int INQUIRY_FLAG = 4; // 询问提示
    public static final int PUSH_MESSAGE_FLAG = 5; // 推送消息提示

    private View view;
    private TextView mInfoTv;
    private ImageView mInfoIcon;

    public ToastShow(Context context) {
        this.context = context;

        view = LayoutInflater.from(context).inflate(R.layout.toast_layout, null, false);
        mInfoTv = (TextView) view.findViewById(R.id.toast_info);
        mInfoIcon = (ImageView) view.findViewById(R.id.toast_img);

        if (toast != null) {
            toast.setView(view);
        }
    }

    /**
     * 单例
     * */
    public static ToastShow getInstance(Context context) {
        if (mToastShow == null) {
            toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            mToastShow = new ToastShow(context);
        }
        return mToastShow;
    }


    /**
     *text: 提示信息
     * flag:提示类型    INFO_FLAG = 0;   基本提示    ERROR_FLAG = 1;   // 错误提示
     * */
    public void toastShow(String text, int flag) {
        if (toast == null) {
            toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            if (view != null) {
                toast.setView(view);
                mInfoTv.setText(text);

                switch (flag){
                    case INFO_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.info_icon);
                        break;
                    case ERROR_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.error_icon);
                        break;
                    case NETWORK_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.netework_error_icon);
                        break;
                    case INQUIRY_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.inquiry_icon);
                        break;
                    case SUCCESS_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.success_icon);
                        break;
                    case PUSH_MESSAGE_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.push_message_info_icon);
                        break;

                }

            }
        } else {

            // 只更新提示内容及Icon,不重新setView
            if (view != null) {
                mInfoTv.setText(text);
                switch (flag){
                    case INFO_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.info_icon);
                        break;
                    case ERROR_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.error_icon);
                        break;
                    case NETWORK_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.netework_error_icon);
                        break;
                    case INQUIRY_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.inquiry_icon);
                        break;
                    case SUCCESS_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.success_icon);
                        break;
                    case PUSH_MESSAGE_FLAG:
                        mInfoIcon.setImageResource(R.mipmap.push_message_info_icon);
                        break;

                }
            }
        }

        toast.show();
    }


    public void toastShow(String text) {
        if (toast == null) {
            toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);
        }
        toast.show();
    }

}